package com.xzq.erp.controller;


import com.xzq.erp.aspect.ApiOperationLog;
import com.xzq.erp.domain.dto.PageDTO;
import com.xzq.erp.domain.dto.UserDTO;
import com.xzq.erp.domain.query.MenuPageQuery;
import com.xzq.erp.domain.query.UserPageQuery;
import com.xzq.erp.domain.vo.UserVO;
import com.xzq.erp.service.ISysUserService;
import com.xzq.erp.utils.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author xzq
 * @since 2025-05-19
 */
@RestController
@RequestMapping("/system/user")
@Tag(name = "用户相关接口")
@RequiredArgsConstructor
public class SysUserController {

    private final ISysUserService sysUserService;

    @GetMapping("/list")
    @Operation(summary = "获取用户列表")
    @ApiOperationLog(description = "获取用户列表")
    @PreAuthorize("hasAuthority('sys:user:list') || hasRole('ADMIN')")
    public Response<PageDTO<UserVO>> getUserList(@ParameterObject @Validated UserPageQuery query) {
        return Response.success(sysUserService.getUserList(query));
    }

    @PostMapping
    @Operation(summary = "添加用户")
    @ApiOperationLog(description = "添加用户")
    @PreAuthorize("hasAuthority('sys:user:add') || hasRole('ADMIN')")
    public Response<Void> addUser(@Validated @RequestBody UserDTO userDTO) {
        sysUserService.addUser(userDTO);
        return Response.success();
    }

    @PutMapping
    @Operation(summary = "修改用户")
    @ApiOperationLog(description = "修改用户")
    @PreAuthorize("hasAuthority('sys:user:update') || hasRole('ADMIN')")
    public Response<Void> updateUser(@Validated @RequestBody UserDTO userDTO) {
        sysUserService.updateUser(userDTO);
        return Response.success();
    }

    @PutMapping("/{id}/password/reset")
    @Operation(summary = "重置用户密码")
    @ApiOperationLog(description = "重置用户密码")
    @PreAuthorize("hasAuthority('sys:user:update') || hasRole('ADMIN')")
    public Response<Void> resetPassword(@PathVariable Long id) {
        sysUserService.resetPassword(id);
        return Response.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户")
    @ApiOperationLog(description = "删除用户")
    @PreAuthorize("hasAuthority('sys:user:delete') || hasRole('ADMIN')")
    public Response<Void> deleteUser(@PathVariable Long id) {
        sysUserService.deleteUser(id);
        return Response.success();
    }

    @GetMapping("/info")
    @Operation(summary = "获取用户信息")
    @ApiOperationLog(description = "获取用户信息")
    public Response<UserVO> getUserInfo() {
        return Response.success(sysUserService.getUserInfo());
    }

    @PutMapping("/password")
    @Operation(summary = "修改密码")
    @ApiOperationLog(description = "修改密码")
    public Response<Void> updatePassword(@RequestBody UserDTO userDTO) {
        sysUserService.updatePassword(userDTO);
        return Response.success();
    }

    @PutMapping("/profile")
    @Operation(summary = "修改当前登录用户信息")
    @ApiOperationLog(description = "修改当前登录用户信息")
    public Response<Void> updateCurrentUser(@RequestBody UserDTO userDTO) {
        sysUserService.updateUser(userDTO);
        return Response.success();
    }
}
